package com.zou.service.impl;


import com.zou.commons.domain.ReturnObject;
import com.zou.commons.utils.DateUtils;
import com.zou.commons.utils.RemoveHtml;
import com.zou.mapper.InfoMapper;
import com.zou.pojo.Info;
import com.zou.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("infoService")
@Transactional
public class InfoServiceImpl implements InfoService {
    @Autowired
    private InfoMapper infoMapper;

    @Override
    public List<Info> querySomeInfoByInfo(Info info) {
        return infoMapper.selectSomeInfoByInfo(info);
    }

    @Override
    public List<Info> queryInfoByMaxId(int id) {
        return infoMapper.findInfoByMaxId(id);
    }

    @Override
    public boolean addInfoHitsById(int id) {
        try {
            if (infoMapper.updateInfoHitsById(id) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public ReturnObject addInfo(Info info) {
        ReturnObject returnObject = new ReturnObject();
        info.setAddDate(DateUtils.formatDateTime(new Date()));
        info.setHits(0); // 默认点击量0
        try {
            if (infoMapper.insertInfo(info) > 0) {
                returnObject.setSuccess();
            } else {
                returnObject.setFail("系统忙，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setFail("系统忙，请稍后重试");
        }
        return returnObject;
    }

    @Override
    public ReturnObject deleteInfoByInfoIds(Integer[] id) {
        ReturnObject returnObject = new ReturnObject();
        try {
            if (infoMapper.deleteInfoByInfoIds(id) > 0) {
                returnObject.setSuccess();
            } else {
                returnObject.setFail("系统忙，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setFail("系统忙，请稍后重试");
        }
        return returnObject;
    }

    @Override
    public ReturnObject editInfo(Info info) {
        ReturnObject returnObject = new ReturnObject();
        try {
            if (infoMapper.updateInfo(info) > 0) {
                returnObject.setSuccess();
            } else {
                returnObject.setFail("系统忙，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setFail("系统忙，请稍后重试");
        }
        return returnObject;
    }

    @Override
    public List<Info> queryInfoByConditionForPage(Map<String, Object> map) {
        System.err.println(map);
        List<Info> infoList = infoMapper.selectInfoByConditionForPage(map);
        // 去除html标签，并截取前十个字符
        for (Info info : infoList) {
            String infoContent = RemoveHtml.removeHtmlTag(info.getContent());
            if (infoContent.length() > 10) {
                infoContent = infoContent.substring(0, 10);
            }
            info.setContent(infoContent);
        }
        return infoList;
    }

    @Override
    public int queryCountOfInfoByCondition(Map<String, Object> map) {
        return infoMapper.selectCountOfInfoByCondition(map);
    }
}
